package com.slwish.xty.mvp.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.slwish.common.mvp.presenter.BasePresenter;
import com.slwish.xty.R;
import com.slwish.xty.config.UrlConfig;
import com.slwish.xty.entity.UserModel;
import com.slwish.xty.mvp.contract.UserInfoContract;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.body.ProgressResponseCallBack;
import com.zhouyou.http.callback.ProgressDialogCallBack;
import com.zhouyou.http.exception.ApiException;

import java.io.File;

import okhttp3.MediaType;

/**
 * 模块说明：
 *
 * @author 陈浩
 * @date 2017/11/30
 */

public class UserInfoPresenter extends BasePresenter<UserInfoContract.View> implements UserInfoContract.Presenter {
    public UserInfoPresenter(UserInfoContract.View view) {
        super(view);
    }

    /**
     * 更新头像
     *
     * @param mContext
     * @param multipartFile
     */
    @Override
    public void uploadPhoto(Context mContext, File multipartFile) {
        EasyHttp.post(UrlConfig.UPLOAD_PHOTO_URL)
                .params("multipartFile", multipartFile, multipartFile.getName(), MediaType.parse("image/*"), new ProgressResponseCallBack() {
                    @Override
                    public void onResponseProgress(long bytesWritten, long contentLength, boolean done) {
//                        LogUtils.i("xx", "bytesWritten:" + bytesWritten);
//                        LogUtils.i("xx", "contentLength:" + contentLength);
//                        LogUtils.i("xx", "done:" + done);
                    }

                }).execute(new ProgressDialogCallBack<String>(showProgressDialog(mContext)) {
            @Override
            public void onSuccess(String s) {
                try {
                    JSONObject jsonObject = JSON.parseObject(s);
                    String resultCode = (String) jsonObject.get("resultCode");
                    String resultMessage = (String) jsonObject.get("resultMessage");
                    //return
                    setError(resultMessage);
                    if (!TextUtils.equals(resultCode, "0"))
                        return;
                    if (getView() != null)
                        getView().uploadPhotoSuccess("");

                } catch (Exception e) {
                    setError(R.string.toast_user_info_upload_photo_exception);
                }
            }

            @Override
            public void onError(ApiException e) {
                super.onError(e);
                setError(R.string.toast_user_info_upload_photo_exception);
            }
        });

    }

    /**
     * 修改性别
     *
     * @param mContext
     * @param sex
     * @param accessToken
     */
    @Override
    public void newSex(final Context mContext, String sex, String accessToken) {
        EasyHttp.post(UrlConfig.NEW_SEX_URL)
                .params("sex", sex)
                .params("accessToken", accessToken)
                .execute(new ProgressDialogCallBack<String>(showProgressDialog(mContext)) {
                    @Override
                    public void onSuccess(String s) {
                        try {
                            JSONObject jsonObject = JSON.parseObject(s);
                            String resultCode = (String) jsonObject.get("resultCode");
                            String resultMessage = (String) jsonObject.get("resultMessage");
                            String user = jsonObject.getString("user");
                            //return
                            setError(resultMessage);
                            if (!TextUtils.equals(resultCode, "0"))
                                return;
                            UserModel model = JSON.parseObject(user, UserModel.class);
                            if (model != null)
                                model.saveOrUpdate("userid = ?", model.getUserId() + "");
//                    if (getView() != null)
//                        getView().uploadPhotoSuccess("");
                        } catch (Exception e) {
                            setError(R.string.toast_user_info_upload_photo_exception);
                        }
                    }

                    @Override
                    public void onError(ApiException e) {
                        super.onError(e);
                        setError(R.string.toast_user_info_upload_photo_exception);
                    }
                });

    }

    /**
     * 更新生日
     *
     * @param mContext
     * @param birthday
     * @param accessToken
     */
    @Override
    public void newBirthday(Context mContext, String birthday, String accessToken) {
        EasyHttp.post(UrlConfig.NEW_BIRTHDAY_URL)
                .params("birthday", birthday)
                .params("accessToken", accessToken)
                .execute(new ProgressDialogCallBack<String>(showProgressDialog(mContext)) {
                    @Override
                    public void onSuccess(String s) {
                        try {
                            JSONObject jsonObject = JSON.parseObject(s);
                            String resultCode = (String) jsonObject.get("resultCode");
                            String resultMessage = (String) jsonObject.get("resultMessage");
                            String user = jsonObject.getString("user");
                            //return
                            setError(resultMessage);
                            if (!TextUtils.equals(resultCode, "0"))
                                return;
                            UserModel model = JSON.parseObject(user, UserModel.class);
                            if (model != null)
                                model.saveOrUpdate("userid = ?", model.getUserId() + "");
//                    if (getView() != null)
//                        getView().uploadPhotoSuccess("");
                        } catch (Exception e) {
                            setError(R.string.toast_user_info_upload_photo_exception);
                        }
                    }

                    @Override
                    public void onError(ApiException e) {
                        super.onError(e);
                        setError(R.string.toast_user_info_upload_photo_exception);
                    }
                });
    }
}
